/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view;

import com.sun.appserv.security.ProgrammaticLogin;
import com.xtd.facade.employment.EmployeeFacade;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.IdleEvent;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean extends AbstractManager implements Serializable {

    @EJB
    EmployeeFacade employeeFacade;
    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;
    private String username;
    private String password;
    private String oldPassword;
    private String newPassword;
    private String confirmPassword;

    /** Creates a new instance of LoginBean */
    public LoginBean() {
    }

    public void closeDialog(CloseEvent closeEvent) {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        if (ec.getUserPrincipal() == null || ec.getUserPrincipal().getName() == null) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "请确认是否已登录，若没有登录，无法使用系统", "");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void changePassword(ActionEvent actionEvent) {

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        boolean success = false;
        System.out.println(oldPassword + " " + newPassword + " " + confirmPassword);
        if (newPassword != null && newPassword.equals(confirmPassword)) {
            success = true;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "已成功修改密码", "");
        } else {
            success = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "修改密码失败", "请重新输入");
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("success", success);

    }

    public void login(ActionEvent actionEvent) {

        RequestContext context = RequestContext.getCurrentInstance();
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest httpRequest = (HttpServletRequest) ec.getRequest();
        HttpServletResponse httpResponse = (HttpServletResponse) ec.getResponse();
        ProgrammaticLogin pl = new ProgrammaticLogin();

        boolean success = false;
        try {

            success = pl.login(username, password, "xtdRealm", httpRequest, httpResponse, true);
        } catch (Exception ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
            addMessage("登录失败", "请重新输入", FacesMessage.SEVERITY_WARN);
        }

        if (success) {
            this.getSessionBean().setUser(employeeFacade.findByUser(username));
            addMessage("登录成功", "欢迎使用新天地婚纱影楼管理系统", FacesMessage.SEVERITY_INFO);
        }
        context.addCallbackParam("success", success);
    }

    public void idleListener(IdleEvent event) {


        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest httpRequest = (HttpServletRequest) ec.getRequest();
        HttpServletResponse httpResponse = (HttpServletResponse) ec.getResponse();
        ProgrammaticLogin pl = new ProgrammaticLogin();
        username = null;
        password = null;
        boolean success = pl.logout(httpRequest, httpResponse);
        addMessage("已退出登录", "请重新登陆");

        //invalidate session
    }

    public void logout(ActionEvent actionEvent) {

        RequestContext context = RequestContext.getCurrentInstance();
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest httpRequest = (HttpServletRequest) ec.getRequest();
        HttpServletResponse httpResponse = (HttpServletResponse) ec.getResponse();
        ProgrammaticLogin pl = new ProgrammaticLogin();

        boolean success = pl.logout(httpRequest, httpResponse);

        if (success) {
            addMessage("退出成功", "欢迎再次使用新天地婚纱影楼管理系统", FacesMessage.SEVERITY_INFO);
        } else {
            addMessage("退出失败", "请稍后再试", FacesMessage.SEVERITY_INFO);
        }
        context.addCallbackParam("success", success);
    }

    /**
     * @return the oldPassword
     */
    public String getOldPassword() {
        return oldPassword;
    }

    /**
     * @param oldPassword the oldPassword to set
     */
    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    /**
     * @return the newPassword
     */
    public String getNewPassword() {
        return newPassword;
    }

    /**
     * @param newPassword the newPassword to set
     */
    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    /**
     * @return the confirmPassword
     */
    public String getConfirmPassword() {
        return confirmPassword;
    }

    /**
     * @param confirmPassword the confirmPassword to set
     */
    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the sessionBean
     */
    public SessionBean getSessionBean() {
        return sessionBean;
    }

    /**
     * @param sessionBean the sessionBean to set
     */
    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }
}
